package com.jsnkj.basic.fragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.jsnkj.basic.R
import com.jsnkj.basic.activity.CustomerServiceActivity
import com.jsnkj.basic.config.SpConfig
import com.jsnkj.basic.activity.InviteFriendsActivity
import com.jsnkj.basic.activity.ReceiptsDetailActivity
import com.jsnkj.basic.activity.RegimentalRecruitmentActivity
import com.jsnkj.basic.activity.SettingActivity
import com.jsnkj.basic.activity.WebViewActivity
import com.jsnkj.basic.activity.WithdrawActivity
import com.jsnkj.basic.adapter.MineAdapter
import com.jsnkj.basic.bean.MineItemBean
import com.jsnkj.basic.databinding.FragmentMineBinding
import com.jsnkj.basic.mvvm.BaseMVVMActivity
import com.jsnkj.basic.mvvm.BaseMVVMFragment
import com.jsnkj.basic.util.ErrorResultUtil
import com.jsnkj.basic.util.SharePreferencesUtil
import com.bumptech.glide.Glide
import com.jeremyliao.liveeventbus.LiveEventBus

class MineFragment : BaseMVVMFragment<FragmentMineBinding, MineViewModel>(), View.OnClickListener {

    companion object {
        fun newInstance() = MineFragment()
    }

    override val layoutId: Int
        get() = R.layout.fragment_mine


    private var homeAdapter: MineAdapter? = null
    private var mineItemBeans: ArrayList<MineItemBean>? = null

    override fun initView(savedInstanceState: Bundle?) {
        mVDB.clickListener = this
        mVDB.rvMine.setLayoutManager(
            LinearLayoutManager(requireActivity(), LinearLayoutManager.VERTICAL, false)
        )

        mineItemBeans = ArrayList()
        // 填充数据
        mineItemBeans?.add(MineItemBean.getInstance(1, R.drawable.icon_income, "收支明细"))
        mineItemBeans?.add(MineItemBean.getInstance(2, R.drawable.icon_invite_friends, "邀请好友"))
        mineItemBeans?.add(
            MineItemBean.getInstance(7, R.drawable.icon_regimental_recruitment, "团长招募")
        )
        mineItemBeans?.add(MineItemBean.getInstance(6, R.drawable.icon_lxkf, "联系客服"))
        mineItemBeans?.add(MineItemBean.getInstance(3, R.drawable.icon_agreement, "用户协议"))
        mineItemBeans?.add(MineItemBean.getInstance(4, R.drawable.icon_privacy_policy, "隐私政策"))
        mineItemBeans?.add(MineItemBean.getInstance(5, R.drawable.icon_setting, "我的设置"))

        homeAdapter = MineAdapter(
            requireActivity(), mineItemBeans!!, object : MineAdapter.onItemClickListener {
                override fun onItemCallBack(position: Int) {
                    when (mineItemBeans!![position].id) {
                        1 -> {
                            //收支明细
                            startActivity(requireActivity(), ReceiptsDetailActivity::class.java)
                        }

                        2 -> {
                            //邀请好友
                            startActivity(requireActivity(), InviteFriendsActivity::class.java)
                        }

                        3 -> {
                            WebViewActivity.startWebActivity(
                                requireActivity(), "用户协议", SpConfig.AGREEMENT_URL
                            )
                        }

                        4 -> {
                            WebViewActivity.startWebActivity(
                                requireActivity(), "隐私政策", SpConfig.PRIVACY_POLICY_URL
                            )
                        }

                        5 -> {
                            startActivity(requireActivity(), SettingActivity::class.java)
                        }

                        6 -> {
                            startActivity(requireActivity(), CustomerServiceActivity::class.java)
                        }

                        7 -> {
                            showDialog()
                            mVM.postUserGroupCode()
                        }
                    }
                }
            })
        mVDB.rvMine.setAdapter(homeAdapter)

        mVM.postLoginResult.observe(this) { user ->
            SharePreferencesUtil.getInstance().getUserBean()?.let {
                it.user = user
                SharePreferencesUtil.getInstance().putUserBean(it)
            }

            if (user.avatar.isNullOrEmpty()){
                Glide.with(requireActivity()).load(R.drawable.icon_user).into(mVDB.imgMineIcon)
            }else{
                Glide.with(requireActivity()).load(user.avatar).into(mVDB.imgMineIcon)
            }

            mVDB.txtMineName.text = user.showName()
            mVDB.txtMineId.text = "会员ID：${user.uuid}"

            if (user.isVipGroup()) {
                mVDB.imgMineCommander.visibility = View.VISIBLE
            } else {
                mVDB.imgMineCommander.visibility = View.GONE
            }

            if (user.parent_uuid.isNullOrEmpty()) {
                mVDB.txtMineMasterId.text = ""
            } else {
                mVDB.txtMineMasterId.text = "| 师傅ID：${user.parent_uuid}"
            }

            mVDB.txtWithdrawalBalance.text = user.withdrawable_coins.toString()
            mVDB.txtMakeMoney.text = user.today_coins.toString()
        }

        mVM.errorResult.observe(this) {
            dismissDialog()
            com.jsnkj.basic.util.LogUtils.e("w我的 ", " 连接失败：$it")
            ErrorResultUtil.errorResultToast(requireActivity() as BaseMVVMActivity<*, *>, it)
        }

        mVM.postUserGroupCodeResult.observe(this) {
            dismissDialog()
            com.jsnkj.basic.util.LogUtils.e("团长信息：${it}")
            it.share_link?.let { share_link ->
                RegimentalRecruitmentActivity.startRecruitmentActivity(
                    requireActivity(), share_link
                )
            }
        }

        LiveEventBus.get<String>("readFragmentRefresh").observe(this) {
            com.jsnkj.basic.util.LogUtils.e("刷新用户信息F")
            mVM.postAuthUserInfo()
        }
    }

    override fun onResume() {
        super.onResume()
        mVDB.tvMineHorseRaceLamp.requestFocus()
        mVM.postAuthUserInfo()
    }


    override fun onClick(view: View) {
        when (view.id) {
            R.id.txt_go_withdrawal_balance -> {
                startActivity(requireActivity(), WithdrawActivity::class.java)
            }

            R.id.img_mine_icon -> {
//                startActivity(requireActivity(), LoginActivity::class.java)
            }
        }
    }


}